package com.litxuf.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

 
public class DateUtil {
	/**
	 * 计算几天后 或几天前的日期
	 * made in 2010-1-15下午08:37:27
	 * @param date
	 * @param i
	 * @param dateFormat
	 * @return
	 *
	 */
	public static String dayAdd(String date, int i, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Calendar c = null;
		try {
			Date date2 = sdf.parse(date);
			c = Calendar.getInstance();
			c.setTime(date2);
			int day = i + c.get(Calendar.DAY_OF_MONTH);
			c.set(Calendar.DAY_OF_MONTH, day);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sdf.format(c.getTime());

	}
	
	 

	public static String monthAdd(String date, int i, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Calendar c = null;
		try {
			Date date2 = sdf.parse(date);
			c = Calendar.getInstance();
			c.setTime(date2);
			int month = i + c.get(Calendar.MONTH);
			c.set(Calendar.MONTH, month);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sdf.format(c.getTime());
	}

	/**
	 * 取出两个时间的之间 天数 包括 开始时间和 结束时间 author xuFeng
	 * 
	 * @param strDate
	 * @param strDate2
	 * @return
	 */
	public static int getInternalDate(String strDate, String strDate2) {
		long invday = 0L;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date d = df.parse(strDate);
			Date d2 = df.parse(strDate2);
			long time = d.getTime();
			long time2 = d2.getTime();
			invday = (time2 - time) / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (int) (invday) + 1;
	}

	/**
	 * 比较两个时间的大小，返回其中大的那个 如：2006-12-13 > 2006-12-10 返回2006-12-13 author xuFeng
	 * 
	 * @param strDate
	 * @param strDate2
	 * @return
	 */
	public static String getCompareDate(String strDate, String strDate2) {
		String res = strDate2;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date d = df.parse(strDate);
			Date d2 = df.parse(strDate2);
			long time = d.getTime();
			long time2 = d2.getTime();
			if (time > time2)
				res = strDate;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return res;
	}

	public static List parseDates(String strDate, String strDate2) {
		List res = new ArrayList();
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date d = df.parse(strDate);
			Date d2 = df.parse(strDate2);
			long time = d.getTime();
			long time2 = d2.getTime();
			long aday = 1000 * 60 * 60 * 24L;
			while (time2 > time) {
				res.add(df.format(new Date(time)));
				time += aday;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 计算两个日期之间包含几周 author xuFeng
	 * 
	 * @param strDate
	 * @param strDate2
	 * @param format
	 * @return
	 */
	public static int getInternalWeek(String strDate, String strDate2,
			String format) {
		int result = 1;
		try {
			long invday = 0l;
			SimpleDateFormat df = new SimpleDateFormat(format);
			Date d = df.parse(strDate);
			Date d2 = df.parse(strDate2);
			long time = d.getTime();
			long time2 = d2.getTime();
			invday = (time2 - time) / (1000 * 60 * 60 * 24);
			int day = (int) (invday) + 1;
			int week = getWeekOfDate(strDate, format);
			if (week == 7)
				week = 0;
			else
				week = week - 1;
			if ((day + week) % 7 != 0) {
				result = (day + week) / 7 + 1;
			} else {
				result = (day + week) / 7;
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 返回是星期几个数值 星期日为 1 星期一为 2
	 */
	public static int getWeekOfDate(String dts, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Calendar cal = Calendar.getInstance();
		Date dt;
		try {
			dt = sdf.parse(dts);
			cal.setTime(dt);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int w = cal.get(Calendar.DAY_OF_WEEK);
		return w;

	}

	/**
	 * 返回当前时间的日期
	 * 
	 * @exception
	 * @return String
	 */
	public static String currDateStr(String pattern) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 返回以当前时间为基点，上个月的第一天日期，格式：yyyy-MM-dd
	 * 
	 * @exception
	 * @return String
	 */
	public static String provDateStr() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * strDate1>strDate2 返回True
	 * made by xufeng
	 * 2010-1-19 上午10:05:27
	 * @param strDate1
	 * @param strDate2
	 * @return
	 */
	public static boolean CompareTwoDate(String strDate1, String strDate2) {
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date d1 = df.parse(strDate1);
			Date d2 = df.parse(strDate2);
			long time1 = d1.getTime();
			long time2 = d2.getTime();
			if (time1 > time2){
				return true;
			}
				 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	 
	 
	public static void main(String[] args) throws Exception {
		
		
		 
	}
}
